#include <iostream>
#include <queue>
#include <string>
using namespace std;

const int N  = 100;

const int n = 30, m = 50;

string g[35];
int dist[35][55];
const int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

struct Node {
    int x, y;
    Node(int xx, int yy) {
        x = xx, y = yy;
    }
};

int bfs() {
    queue<Node> q;
    q.push({0, 0});
    dist[0][0] = 0;
    while (q.size()) {
        Node node = q.front();
        int x = node.x, y = node.y;
        q.pop();
        if (x == n - 1 && y == m - 1) {
            return dist[x][y];
        }
        for (int i = 0; i < 4; ++i) {
            int xx = x + dir[i][0], yy = y + dir[i][1];

            if (xx < 0 || xx > n - 1 || yy < 0 || yy > m - 1 || g[xx][yy] == '1') {
                continue;
            }

            q.push({xx, yy});
            dist[xx][yy] = dist[x][y] + 1;
        }
    }
    return -1;
}

int main()
{
    // 请在此输入您的代码

    for (int i = 0; i < n; ++i) {
        cin >> g[i];
    }

    cout << bfs() << endl;

    return 0;
}